cli: fix concurrent mops invocations clobbering shared scratch dirs#532
Merged
Conversation
`mops check-stable` shared `.mops/.check-stable/`, and `mops check`/`build`/`check-stable` shared `<parent>/.migrations-<canister>/` for migration staging. Two processes running on the same project (e.g. an editor watcher + `caffeine check --fix`) raced on these dirs and surfaced as `new.most: No such file or directory` or `EEXIST: file already exists, symlink`. Switch both to per-invocation `mkdtempSync` dirs so each process owns its own scratch space and removes it in `finally`. No coordination needed. Co-authored-by: Cursor <cursoragent@cursor.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Running two
mopsprocesses on the same project (e.g. an editor watcher andcaffeine check --fix, or two back-to-back invocations from a script) intermittently failed with misleading errors:.mops/.check-stable/new.most: No such file or directoryfrommops check-stableEEXIST: file already exists, symlink ...frommops check/build/check-stablewhen[canisters.<name>.migrations]was configuredWorse,
check-stablewould print a "Hint: You may need a migration" suggestion (now removed in #531) that pointed users at the wrong cause entirely.Root cause
Two scratch directories were shared across all
mopsprocesses for the same project:.mops/.check-stable/—runStableCheckdidrm -rf <dir>→mkdir <dir>at the start andrm -rf <dir>at the end. Two concurrent runs would: A createsnew.most, B's startuprm -rfdeletes it, A's subsequent--stable-compatibleread fails.<parent>/.migrations-<canister>/—prepareMigrationArgs(used when migrations need a trimmed/next-merged staging dir) wiped the dir and re-symlinked. Two concurrent runs raced onsymlinkSync(EEXIST) or on each other's wipe.Both directories existed mostly to give moc a stable path to read from during the invocation; they weren't actually shared state that needed to be coordinated.
Fix
Switch both directories to per-invocation unique paths created with
fs.mkdtempSync:.mops/.check-stable/→.mops/.check-stable-XXXXXX/<parent>/.migrations-<canister>/→<parent>/.migrations-<canister>-XXXXXX/Each process owns its own dir for its lifetime and removes it on exit. No shared mutable state → no race possible by construction. Existing
.gitignorerules already cover both patterns (.mops/and.migrations-*/).Caller behavior is unchanged:
prepareMigrationArgsstill returns acleanup()callback that removes the staged dir infinally.Caveats
.most/.wasmfiles). Both locations are gitignored, so it's cosmetic..mops/.check-stable/or<parent>/.migrations-<canister>/directories from older releases aren't auto-cleaned. They'll stay until the user removes them; harmless but visible ingit statusfor projects that don't ignore them.Test plan
mops check-stableinvocations against a fixture withcheck-limit = 3and 4 chain migrations (forces both the check-stable scratch path and the migration stagingmkdtempbranch).npm run lint+npm run checkclean.mops check-stable(orcaffeine check --fix+ an editor watcher) on a project with[migrations]configured; confirm clean output.Made with Cursor